<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\CommonBase;
use think\facade\View;
use think\response\Json;
use app\common\model\ModuleField as ModuleFieldModel;

class FeedbackField extends AdminBase
{
    public function lists(): string
    {
        $formId = $this->request->param('form_id', 0, 'intval');
        if (empty($formId)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $feedbackBusiness = new \app\admin\business\FeedbackForm($this->websiteCode);
        $feedbackForm     = $feedbackBusiness->findFeedbackFormById($formId);
        if (empty($feedbackForm)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        View::assign([
            'formId'              => $formId,
            'feedbackFormName'    => $feedbackForm['name'],
            'feedbackFormListUrl' => url('feedbackForm/lists', ['website_code' => $this->websiteCode])]);

        return View::fetch();
    }

    public function list(): Json
    {
        $formId                = $this->request->param('form_id', 0, 'intval');
        $page                  = $this->request->param('page', 1, 'intval');
        $limit                 = $this->request->param('limit', 10, 'intval');
        $name                  = $this->request->param('name', '', 'trim');
        $status                = $this->request->param('status', 0, 'intval');
        $feedbackFieldBusiness = new \app\admin\business\FeedbackField($this->websiteCode);

        $where = ['form_id' => $formId];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }

        $adPositionInfo = $feedbackFieldBusiness->getFeedbackFieldList($page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $adPositionInfo['rows'], $adPositionInfo['count']);
    }

    public function add(): string
    {
        $formId = $this->request->param('form_id', 0, 'intval');
        if (empty($formId)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        View::assign(['formId' => $formId]);
        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $formId    = $this->request->param('form_id', 0, 'intval');
        $sort      = $this->request->param("sort", 0, 'intval');
        $name      = $this->request->param('name', '', 'trim');
        $code      = $this->request->param('code', '', 'trim');
        $status    = $this->request->param('status', 0, 'intval');
        $isRequire = $this->request->param('is_require', 0, 'intval');
        $fieldType = $this->request->param('field_type', '', 'trim');

        $data     = [
            'name'         => $name,
            'status'       => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'code'         => $code,
            'is_system'    => CommonBase::FIELD_IS_CUSTOMER,
            'show_list'    => CommonBase::FIELD_IS_HIDDEN_LIST,
            'form_id'      => $formId,
            'sort'         => $sort,
            'website_code' => $this->websiteCode,
            'is_require'   => $isRequire == CommonBase::FIELD_IS_REQUIRE ? CommonBase::FIELD_IS_REQUIRE : CommonBase::FIELD_IS_NOT_REQUIRE,
            'field_type'   => $fieldType,
        ];
        $validate = (new \app\admin\validate\FeedbackField())->scene('add');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $feedbackFieldBusiness = new \app\admin\business\FeedbackField($this->websiteCode);
        $res                   = $feedbackFieldBusiness->addFeedbackField($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $feedbackFieldBusiness = new \app\admin\business\FeedbackField($this->websiteCode);
        $feedbackField         = $feedbackFieldBusiness->findFeedbackFieldById($id);
        if (empty($feedbackField)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(["feedbackField" => $feedbackField]);
        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id        = $this->request->param('id', 0, 'intval');
        $sort      = $this->request->param("sort", 0, 'intval');
        $name      = $this->request->param('name', '', 'trim');
        $status    = $this->request->param('status', 0, 'intval');
        $isRequire = $this->request->param('is_require', '', 'intval');

        $feedbackFieldBusiness = new \app\admin\business\FeedbackField($this->websiteCode);
        $feedbackField         = $feedbackFieldBusiness->findFeedbackFieldById($id);

        $data = ['name' => $name, 'sort' => $sort, 'status' => self::_IS_ENABLE_STATUS];
        if ($feedbackField['is_system'] != CommonBase::FIELD_IS_SYSTEM) {
            $data['status']     = $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS;
            $data['is_require'] = $isRequire == CommonBase::FIELD_IS_REQUIRE ? CommonBase::FIELD_IS_REQUIRE : CommonBase::FIELD_IS_NOT_REQUIRE;
        }
        $validate = (new \app\admin\validate\FeedbackField())->scene('edit');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $feedbackFieldBusiness = new \app\admin\business\FeedbackField($this->websiteCode);
        $res                   = $feedbackFieldBusiness->updateFeedbackField($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }


    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $feedbackFieldBusiness = new \app\admin\business\FeedbackField($this->websiteCode);
        $res                   = $feedbackFieldBusiness->deleteFeedbackField($id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doSort(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $ids  = $this->request->param('ids', '', 'trim');
        $sort = $this->request->param('sort', '', 'trim');
        if (empty($ids) || empty($sort)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $idArr = explode(',', $ids);
        foreach ($idArr as $k => $v) {
            $idArr[$k] = intval($v);
        }

        $sortArr = explode(',', $sort);
        foreach ($sortArr as $k => $v) {
            $sortArr[$k] = intval($v);
        }

        $data = [];
        foreach ($idArr as $k => $v) {
            $data[$v] = $sortArr[$k];
        }

        $feedbackFieldBusiness = new \app\admin\business\FeedbackField($this->websiteCode);
        $res                   = $feedbackFieldBusiness->batchFeedbackFieldSort($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }
}
